﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI;
using RN.ERP.Web.AuthorityService;
using RN.ERP.Web.ModelService;
using Newtonsoft.Json;


namespace RN.ERP.Web
{
    public class BasePage : System.Web.UI.Page
    {
        #region Session数据
        public string SystemID
        {
            get
            {
                return ConfigurationManager.AppSettings["AppId"];
            }
        }
        protected SessionUserInfo UserInfo
        {
            get
            {
                if (Session[SessionConstant.SESSION_USERINFO] == null)
                {
                    RebuildSessionUserInfo();
                }
                return Session[SessionConstant.SESSION_USERINFO] == null ? null : (SessionUserInfo)Session[SessionConstant.SESSION_USERINFO];
            }
        }

        #endregion

        #region Page属性

        public string PageTitle
        {
            get
            {
                return ConfigurationManager.AppSettings["sysTitle"];
            }
        }

        protected string PageCode
        {
            get
            {
                return this.GetType().Name.Trim().ToLower();
            }
        }

        public virtual bool IsCheckPermission
        {
            get { return false; }
        }


        public string SystemType
        {
            get
            {
                return ConfigurationManager.AppSettings["SystemType"];
            }
        }

        #endregion

        #region Page事件

        protected override void OnInit(EventArgs e)
        {
            this.Response.Expires = 0;
            base.OnInit(e);
            CheckLogin();
            if (IsCheckPermission)
            {
                CheckFuncPermission();
            }
            InitControls();
        }

        #endregion

        #region 一般方法

        /// <summary>
        /// 检查用户是否登录。
        /// </summary>
        protected void CheckLogin()
        {
            if (UserInfo == null)
            {
                Response.Redirect("~/Login.aspx");
            }
        }

        /// <summary>
        /// 检查用户是否拥有此权限。
        /// </summary>
        protected void CheckFuncPermission()
        {
            var mappings = (List<ModelService.T_Sys_FuncPageMapping>)Application[ApplicationConstant.APPLICATION_FUNCPAGEMAPPING];
            if (mappings != null)
            {
                ModelService.T_Sys_FuncPageMapping mapping = mappings.SingleOrDefault(m => m.PageCode == PageCode);
                if (mapping != null)
                {
                    if (!UserInfo.FuncPermCollection.Contains(mapping.FuncCodes.ToList()))
                    {
                        //ClearSessionUserInfo();
                        Response.Redirect("~/error.aspx?c=e01");
                    }
                }
                else
                {
                    ////没有此页面的配置。
                    ClearSessionUserInfo();
                    Response.Redirect("~/Login.aspx");
                }
            }
            else
            {
                ////没有配置权限。
                ClearSessionUserInfo();
                Response.Redirect("~/Login.aspx");
            }
        }

        /// <summary>
        /// 初始化页面的控件。
        /// </summary>
        protected virtual void InitControls()
        {

        }

        /// <summary>
        /// 重构用户Session数据。
        /// </summary>
        private void RebuildSessionUserInfo()
        {
            UtilitiesTool.RebuildSessionUserInfo();
        }

        protected void ClearSessionUserInfo()
        {
            UtilitiesTool.ClearSessionUserInfo();
        }

        #endregion

        public string _flowClassId = string.Empty;
        /// <summary>
        /// 流程类型标识
        /// </summary>
        public string FlowClassId
        {
            get
            {
                int classid = 0;
                int.TryParse(_flowClassId, out classid);
                _flowClassId = (Request.QueryString["classid"] == null || Request.QueryString["classid"].ToString() == "" || ("-1" == Request.QueryString["classid"].ToString() && classid > 0) || ("0" == Request.QueryString["classid"].ToString() && classid > 0)) ? _flowClassId : Request.QueryString["classid"].ToString();
                return _flowClassId;
            }
            set
            {
                _flowClassId = value;
            }
        }

        public string GetPrizeFlag(object prizeflag)
        {
            if (prizeflag != null)
            {
                switch (Convert.ToInt32(prizeflag))
                {
                    case 1:
                        return "质量或技术进步";
                    case 2:
                        return "安全或文明施工";
                    case 3:
                        return "其它";
                    default:
                        return "其它";
                }
            }
            else
            {
                return "其它";
            }
        }

        public string AppID
        {
            get
            {
                return ConfigurationManager.AppSettings["AppID"] ?? "";
            }
        }

        public string SysAreaCode
        {
            get
            {
                return ConfigurationManager.AppSettings["sysArea"] ?? "";
            }
        }

        public string GetKouFenType(object submit)
        {
            if (submit != null)
            {
                switch (submit.ToString())
                {
                    case "1":
                        return "质量";
                    case "2":
                        return "安全";
                    case "3":
                        return "分包及工资";
                    case "4":
                        return "其他";
                    default:
                        return "";
                }
            }
            else
            {
                return string.Empty;
            }
        }

        public bool isCSAdmin
        {
            get
            {
                string csAdminRole = ConfigurationManager.AppSettings["khrole1"];
                string[] csrolearr = csAdminRole.Split(',');
                List<string> csadminrolelist = new List<string>(csrolearr);
                if (csadminrolelist.Contains(UserInfo.RoleId))
                {
                    return true;
                }
                return false;
            }
        }

        public bool isFSAdmin
        {
            get
            {
                string fsAdminRole = ConfigurationManager.AppSettings["khrole2"];
                string[] fsrolearr = fsAdminRole.Split(',');
                List<string> fsadminrolelist = new List<string>(fsrolearr);
                if (fsadminrolelist.Contains(UserInfo.RoleId))
                {
                    return true;
                }
                return false;
            }
        }

        /// <summary>
        /// 是否是青岛工程处主管
        /// </summary>
        public bool isGCCAdmin
        {
            get
            {
                string gccAdminDogid = ConfigurationManager.AppSettings["gccDogID"];
                string[] gccdogid = gccAdminDogid.Split(',');
                List<string> gccAdminlist = new List<string>(gccdogid);
                if (gccAdminlist.Contains(UserInfo.DogId))
                {
                    return true;
                }
                return false;
            }
        }

        public string SubmitDepartment
        {
            get
            {
                string zjzAdminDogid = ConfigurationManager.AppSettings["zjzDogID"];////质量
                string[] zjzdogid = zjzAdminDogid.Split(',');
                List<string> zjzAdminlist = new List<string>(zjzdogid);
                if (zjzAdminlist.Contains(UserInfo.DogId))
                {
                    return "1";
                }
                string ajzAdminDogid = ConfigurationManager.AppSettings["ajzDogID"];////安全
                string[] ajzdogid = ajzAdminDogid.Split(',');
                List<string> ajzAdminlist = new List<string>(ajzdogid);
                if (ajzAdminlist.Contains(UserInfo.DogId))
                {
                    return "2";
                }
                string jgcAdminDogid = ConfigurationManager.AppSettings["jgcDogID"];////分包及工资
                string[] jgcdogid = jgcAdminDogid.Split(',');
                List<string> jgcAdminlist = new List<string>(jgcdogid);
                if (jgcAdminlist.Contains(UserInfo.DogId))
                {
                    return "3";
                }
                string gccAdminDogid = ConfigurationManager.AppSettings["gccDogID"];////其他
                string[] gccdogid = gccAdminDogid.Split(',');
                List<string> gccAdminlist = new List<string>(gccdogid);
                if (gccAdminlist.Contains(UserInfo.DogId))
                {
                    return "4";
                }
                return string.Empty;
            }
        }

        ///// <summary>
        ///// 查找地区下，某角色参与其中的某个类型的所有流程id,若roleID为空，返回某个类型的所有流程id
        ///// </summary>
        ///// <param name="areaCode"></param>
        ///// <param name="type"></param>
        ///// <param name="roleID"></param>
        ///// <returns></returns>
        //public List<string> GetClassIDListbyType(string areaCode, string type, string roleID, out string msg)
        //{
        //    return FlowCommon.FlowHelper.GetClassIDListbyType(areaCode, type, roleID, out msg);
        //}
    }
}
